<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\controller;

use app\BaseController;
use app\enum\CodeEnum;
use app\service\fetch\OceanFetchService;
use app\service\OeFetchService;
use think\response\Json;

class OeFetchController extends BaseController
{
    /**
     * 拉取应用分包.
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @deprecated
     */
    public function fetchAppExtend(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        $res = OeFetchService::fetchAppExtend($advertiserId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }

    /**
     * 同步抖音授权关系.
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fetchAwemeList(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        $res = OeFetchService::fetchAwemeList($advertiserId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }



    /**
     * 同步获取可用优化目标.
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fetchOptimizedGoal(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        $res = OeFetchService::fetchOptimizedGoal($advertiserId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }



    /**
     * 同步账户下原生锚点.
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @deprecated
     */
    public function fetchNativeAnchor(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        $res = OeFetchService::fetchNativeAnchor($advertiserId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }


    /**
     * 同步获取抖音主页视频.
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fetchAwemeVideo(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        $awemeId = request()->post('aweme_id', '');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        $res = OeFetchService::fetchAwemeVideo($advertiserId, $awemeId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }

    /**
     * 实时获取橙子落地页.
     */
    public function getOrangeSite(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        $externalAction = request()->post('external_action', '');
        $deepExternalAction = request()->post('deep_external_action', '');
        $searchMode = request()->post('search_mode', '');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        if (! $externalAction) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '优化目标不能为空');
        }
        $res = OeFetchService::getOrangeSite($advertiserId, $externalAction, $deepExternalAction, $searchMode);
        if ($res !== false) {
            return self::outputSuccess($res);
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OeFetchService::getError(0));
    }

    /**
     * 同步获取产品.
     * @return Json
     */
    public function getProduct(): Json
    {
        $advertiserId = request()->post('advertiser_id');
        if (! $advertiserId) {
            return self::outputFail(CodeEnum::BUSINESS_ERROR, '账户id不能为空');
        }
        OceanFetchService::fetchProductPlatform($advertiserId);
        $res = OceanFetchService::fetchProductList($advertiserId);
        if ($res) {
            return self::outputSuccess();
        }
        return self::outputFail(CodeEnum::BUSINESS_ERROR, OceanFetchService::getError(0));
    }
}
